package com.promise.jokerdream.ui.settings

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.clickable
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.promise.jokerdream.database.entity.EscortGameConfigEntity
import com.promise.jokerdream.config.game.EscortStrategy
import com.promise.jokerdream.ui.components.SettingCard
import com.promise.jokerdream.ui.components.RoundCheckbox
import com.promise.jokerdream.ui.components.AppChip
import com.promise.jokerdream.ui.theme.AppSpacing
import com.promise.jokerdream.ui.theme.AppFontSizes

/**
 * 押镖设置卡片
 */
@Composable
fun PenguinEscortSettingCard(
    config: EscortGameConfigEntity,
    onEnabledChange: (Boolean) -> Unit,
    onNpcIdsChange: (List<Int>) -> Unit,
    onStrategyChange: (EscortStrategy) -> Unit,
    modifier: Modifier = Modifier,
    onExecuteClick: (() -> Unit)? = null
) {
    SettingCard(
        title = "押镖",
        description = "",
        checked = config.enabledSetting,
        onCheckedChange = onEnabledChange,
        modifier = modifier,
        onExecuteClick = onExecuteClick,
        content = {
            if (config.enabledSetting) {
                // 转换 selectedNpcIds: String -> List<Int>
                val selectedNpcIds = if (config.selectedNpcIds.isEmpty()) {
                    emptyList()
                } else {
                    config.selectedNpcIds.split(",").mapNotNull { it.trim().toIntOrNull() }
                }
                // 转换 strategy: String -> EscortStrategy
                val strategy = try {
                    EscortStrategy.valueOf(config.strategy)
                } catch (e: Exception) {
                    EscortStrategy.DIRECT_START
                }
                // 押镖设置区域
                EscortSettingsArea(
                    selectedNpcIds = selectedNpcIds,
                    strategy = strategy,
                    onNpcIdsChange = onNpcIdsChange,
                    onStrategyChange = onStrategyChange
                )
            }
        }
    )
}

/**
 * 押镖设置区域
 */
@Composable
private fun EscortSettingsArea(
    selectedNpcIds: List<Int>,
    strategy: EscortStrategy,
    onNpcIdsChange: (List<Int>) -> Unit,
    onStrategyChange: (EscortStrategy) -> Unit
) {
    Column(
        verticalArrangement = Arrangement.spacedBy(AppSpacing.componentMedium)
    ) {
        // NPC选择区域
        NpcSelectionArea(
            selectedNpcIds = selectedNpcIds,
            onNpcIdsChange = onNpcIdsChange
        )
        
        // 押镖策略选择
        EscortStrategyArea(
            strategy = strategy,
            onStrategyChange = onStrategyChange
        )
    }
}

/**
 * NPC选择区域
 */
@Composable
private fun NpcSelectionArea(
    selectedNpcIds: List<Int>,
    onNpcIdsChange: (List<Int>) -> Unit
) {
    val npcOptions = listOf(
        0 to "蔡八斗",
        1 to "吕青橙", 
        2 to "温良恭",
        3 to "老板娘"
    )
    
    Column {
        Text(
            text = "选择镖师:",
            fontSize = AppFontSizes.mediumBody,
            color = MaterialTheme.colorScheme.onSurface
        )
        
        Spacer(modifier = Modifier.height(AppSpacing.componentSmall))
        
        LazyRow(
            horizontalArrangement = Arrangement.spacedBy(AppSpacing.componentMedium)
        ) {
            items(npcOptions) { (npcId, npcName) ->
                val isSelected = selectedNpcIds.contains(npcId)
                
                AppChip(
                    text = npcName,
                    isSelected = isSelected,
                    onClick = {
                        val newSelectedIds = if (isSelected) {
                            selectedNpcIds - npcId
                        } else {
                            selectedNpcIds + npcId
                        }
                        onNpcIdsChange(newSelectedIds)
                    }
                )
            }
        }
    }
}

/**
 * 押镖策略选择区域
 */
@Composable
private fun EscortStrategyArea(
    strategy: EscortStrategy,
    onStrategyChange: (EscortStrategy) -> Unit
) {
    Column {
        Row(
            modifier = Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.spacedBy(AppSpacing.componentMedium)
        ) {
            Text(
                text = "押镖策略:",
                fontSize = AppFontSizes.mediumBody,
                color = MaterialTheme.colorScheme.onSurface
            )
            
            Text(
                text = "(无免费次数时)",
                fontSize = AppFontSizes.smallBody,
                color = MaterialTheme.colorScheme.onSurfaceVariant
            )
        }
        
        Spacer(modifier = Modifier.height(AppSpacing.componentSmall))
        
        // 两个策略选项在同一行
        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.spacedBy(AppSpacing.componentMedium)
        ) {
            // 直接开始押镖选项
            Row(
                modifier = Modifier
                    .weight(1f)
                    .clickable { onStrategyChange(EscortStrategy.DIRECT_START) }
                    .padding(vertical = AppSpacing.componentSmall),
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.spacedBy(AppSpacing.componentSmall)
            ) {
                RoundCheckbox(
                    checked = strategy == EscortStrategy.DIRECT_START,
                    onCheckedChange = { onStrategyChange(EscortStrategy.DIRECT_START) }
                )
                
                Text(
                    text = "直接发车",
                    fontSize = AppFontSizes.mediumBody,
                    color = MaterialTheme.colorScheme.onSurface
                )
            }
            
            // 选择老板娘后开始押镖选项
            Row(
                modifier = Modifier
                    .weight(1f)
                    .clickable { onStrategyChange(EscortStrategy.CHOOSE_BOSS_LADY) }
                    .padding(vertical = AppSpacing.componentSmall),
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.spacedBy(AppSpacing.componentSmall)
            ) {
                RoundCheckbox(
                    checked = strategy == EscortStrategy.CHOOSE_BOSS_LADY,
                    onCheckedChange = { onStrategyChange(EscortStrategy.CHOOSE_BOSS_LADY) }
                )
                
                Text(
                    text = "选老板娘发车",
                    fontSize = AppFontSizes.mediumBody,
                    color = MaterialTheme.colorScheme.onSurface
                )
            }
        }
    }
}

